﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using DDDSample.Domain.Cargo;
using DDDSample.Domain.HandlingEvent;

namespace DDDSample.UI.Mvc.Models
{
    public class CargoTrackingViewModel
    {
        private readonly Cargo _cargo;
        private readonly IList<HandlingEvent> _handlingEvents;

        public CargoTrackingViewModel(Cargo cargo, HandlingHistory handlingHistory)
        {
            _cargo = cargo;
            _handlingEvents = handlingHistory != null ? handlingHistory.EventsByCompletionTime.ToList() : new List<HandlingEvent>();
        }

        public string Id
        {
            get { return _cargo.Id.ToString(); }
        }

        public IEnumerable<HandlingEventViewModel> HandlingEvents
        {
            get { return _handlingEvents.Select(x => new HandlingEventViewModel(x, _cargo)); }
        }

        public string StatusText
        {
            get
            {
                switch (_cargo.Delivery.TransportStatus)
                {
                    case TransportStatus.InPort:
                        return Resources.Messages.cargo_status_IN_PORT.UIFormat(_cargo.Delivery.LastKnownLocation.Name);
                    case TransportStatus.OnboardCarrier:
                        return Resources.Messages.cargo_status_ONBOARD_CARRIER.UIFormat("XXX");
                    case TransportStatus.Claimed:
                        return Resources.Messages.cargo_status_CLAIMED.UIFormat();
                    case TransportStatus.NotReceived:
                        return Resources.Messages.cargo_status_NOT_RECEIVED.UIFormat();
                    case TransportStatus.Unknown:
                        return Resources.Messages.cargo_status_UNKNOWN.UIFormat();
                }
                throw new NotSupportedException();
            }
        }

        public String NextExpectedActivity
        {
            get
            {
                var activity = _cargo.Delivery.NextExpectedActivity;
                if (activity == null)
                {
                    return "";
                }

                const string text = "Next expected activity is to ";
                var type = activity.EventType;
                if (type == HandlingEventType.Load)
                {
                    return
                       text + type.ToString().ToLower() + " cargo onto voyage XXX" +
                       " in " + activity.Location.Name;
                }
                if (type == HandlingEventType.Unload)
                {
                    return
                       text + type.ToString().ToLower() + " cargo off of XXX" +
                       " in " + activity.Location.Name;
                }
                return text + type.ToString().ToLower() + " cargo in " + activity.Location.Name;
            }
        }

        public string Destination
        {
            get
            {
                return _cargo.RouteSpecification.Destination.Name;
            }
        }

        public string Eta
        {
            get
            {
                var eta = _cargo.Delivery.EstimatedTimeOfArrival;
                return eta.HasValue ? eta.ToString() : "?";
            }
        }
    }

    public static class StringExtensions
    {
        /// <summary>
        /// Returns string formatted using this format string and provided arguments.
        /// </summary>
        /// <param name="formatString"></param>
        /// <param name="arguments"></param>
        /// <returns></returns>
        public static string UIFormat(this string formatString, params object[] arguments)
        {
            return string.Format(CultureInfo.CurrentCulture, formatString, arguments);
        }
    }
}